package com.zj.blessings.util;

import java.util.ArrayList;
import java.util.List;

import cn.waps.br;

import com.zj.blessings.db.DBItem;
import com.zj.blessings.db.DBLikeItem;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class LikeDbHelper {
	public static final String KEY_ROWID = "_id"; // 数据表字段
	public static final String KEY_TITLE = "title"; // 数据表字段
	public static final String KEY_TEXT = "text"; // 数据表字段
	public static final String KEY_URL = "url"; // 数据表字段
	public static final String KEY_SAVE_TIME = "save_time"; // 数据表字段
	private DatabaseHelper mDbHelper; // SQLiteOpenHelper实例对象
	private SQLiteDatabase mDb; // 数据库实例对象

	private static final String DATABASE_CREATE = "create table DBLikeItem (_id integer primary key autoincrement, "
			+ "title text , text text , url text ,save_time long );";
	private static final String DATABASE_NAME = "like.db"; // 数据库名
	private static final String DATABASE_TABLE = "DBLikeItem"; // 数据库表名
	private static final int DATABASE_VERSION = 1; // 数据库版本号
	private final Context mCtx; // 上下文实例

	private static class DatabaseHelper extends SQLiteOpenHelper { // 数据库辅助类
		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(DATABASE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS DBLikeItem");
			onCreate(db);
		}
	}

	public LikeDbHelper(Context ctx) {
		this.mCtx = ctx;
	}

	public LikeDbHelper open() throws SQLException {
		mDbHelper = new DatabaseHelper(mCtx);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		mDbHelper.close();
	}

	public long addLikeItem(DBItem dbItem) {
		long n = 0;
		boolean isFind = false;
		List<DBLikeItem> list = getLikeItems();
		for (DBLikeItem dbLikeItem : list) {
			if (dbLikeItem.title.equals(dbItem.title)) {
				isFind = true;
				break;
			}
		}
		if (!isFind) {
			open();
			ContentValues initialValues = new ContentValues();
			initialValues.put(KEY_TITLE, dbItem.title);
			initialValues.put(KEY_TEXT, dbItem.text);
			initialValues.put(KEY_URL, dbItem.url);
			initialValues.put(KEY_SAVE_TIME, System.currentTimeMillis());
			n = mDb.insert(DATABASE_TABLE, null, initialValues);
			close();
		}
		return n;
	}

	public boolean deleteLikeItem(DBLikeItem dbLikeItem) {
		open();
		boolean b = mDb.delete(DATABASE_TABLE, "title=?",
				new String[] { dbLikeItem.title }) > 0;
		close();
		return b;
	}

	public List<DBLikeItem> getLikeItems() {
		try {
			open();
			Cursor cursor = mDb.query(DATABASE_TABLE, null, null, null, null,
					null, "_id desc");
			List<DBLikeItem> list = new ArrayList<DBLikeItem>();
			while (cursor != null && cursor.moveToNext()) {
				DBLikeItem item = new DBLikeItem();
				item.title = cursor.getString(cursor.getColumnIndex(KEY_TITLE));
				item.text = cursor.getString(cursor.getColumnIndex(KEY_TEXT));
				item.url = cursor.getString(cursor.getColumnIndex(KEY_URL));
				item.save_time = cursor.getLong(cursor
						.getColumnIndex(KEY_SAVE_TIME));
				list.add(item);
			}
			if (cursor != null) {
				cursor.close();
			}
			close();
			return list;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	// public Cursor fetchNote(long rowId) throws SQLException {
	// Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[] {
	// KEY_ROWID, KEY_TEXT, KEY_TITLE }, KEY_ROWID + "=" + rowId,
	// null, null, null, null, null);
	// if (mCursor != null) {
	// mCursor.moveToFirst();
	// }
	// return mCursor;
	// }

	// public boolean updateNote(long rowId, String title, String body) {
	// ContentValues args = new ContentValues();
	// args.put(KEY_TEXT, title);
	// args.put(KEY_TITLE, body);
	// return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) >
	// 0;
	// }
}
